﻿using ClientAppraise.AutoMapperProfile;
using ClientAppraise.Services;
using ClientAppraise.Services.Impl;
using ClientAppraise.ViewModels.Pages;
using ClientAppraise.ViewModels.Pages.Users;
using ClientAppraise.Views;
using ClientAppraise.Views.Pages;
using ClientAppraise.Views.Pages.Users;
using ClientBll;
using ClientDao;
using ClientInfrastructure.Config;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ShareCode.Entity;
using System;
using System.Configuration;
using System.Data;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.JavaScript;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using Wpf.Ui;

namespace ClientAppraise
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {

        // public static INavigationService navigationService = new NavigationService();

        private static readonly IHost _host = Host.CreateDefaultBuilder()
       .ConfigureAppConfiguration(c =>
       {
           c.SetBasePath(AppContext.BaseDirectory);
       })
       .ConfigureServices(
           (_, services) =>
           {
               // App Host
               services.AddHostedService<ApplicationHostService>();

               // Main window container with navigation
               services.AddSingleton<LoginView>();
               services.AddSingleton<LoginViewModel>();

               services.AddSingleton<MainWindow>();
               services.AddSingleton<MainWindowViewModel>();

               services.AddSingleton<IZxNavigationService, ZxNavigationService>();
               services.AddSingleton<IZxSnackbarService, ZxSnackbarService>();
               services.AddSingleton<IZxContentDialogService, ZxContentDialogService>();
               services.AddSingleton<IZxDrawerService, ZxDrawerService>();
               services.AddSingleton<IZxTitleRightService, ZxTitleRightService>();
               services.AddSingleton<WindowsProviderService>();


               // All other pages and view models
               services.AddTransientFromNamespace("ClientAppraise.Views.Pages", typeof(App).Assembly);
               services.AddTransientFromNamespace("ClientAppraise.ViewModels.Pages", typeof(App).Assembly);
               services.AddTransientFromNamespace("ClientDao", typeof(DaoAssembly).Assembly);
               services.AddTransientFromNamespace("ClientBll", typeof(BaseBll).Assembly);
               services.AddTransientFromNamespace("ShareCode.Entity", typeof(BaseBaseEntity).Assembly);
           }
       )
       .Build();

        public static IZxSnackbarService MySnackbar = GetRequiredService<IZxSnackbarService>();
        public static IZxContentDialogService MyContentDialog = GetRequiredService<IZxContentDialogService>();
        public static IZxDrawerService MyDrawer = GetRequiredService<IZxDrawerService>();
        public static IZxNavigationService MyNavigation = GetRequiredService<IZxNavigationService>();
        public static IZxTitleRightService TitleRight = GetRequiredService<IZxTitleRightService>();


        /// <summary>
        /// Gets registered service.
        /// </summary>
        /// <typeparam name="T">Type of the service to get.</typeparam>
        /// <returns>Instance of the service or <see langword="null"/>.</returns>
        public static T GetRequiredService<T>()
            where T : class
        {
            return _host.Services.GetRequiredService<T>();
        }

        /// <summary>
        /// Occurs when the application is loading.
        /// </summary>
        private void OnStartup(object sender, StartupEventArgs e)
        {
            BaseAutoMapperProfile.MapperProfile();
            _host.Start();
        }

        /// <summary>
        /// Occurs when the application is closing.
        /// </summary>
        private void OnExit(object sender, ExitEventArgs e)
        {
            _host.StopAsync().Wait();

            _host.Dispose();
        }

        /// <summary>
        /// Occurs when an exception is thrown by an application but not handled.
        /// </summary>
        private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            // For more info see https://docs.microsoft.com/en-us/dotnet/api/system.windows.application.dispatcherunhandledexception?view=windowsdesktop-6.0
        }
    }

}
